package com.itheima.controller.backend.security;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckSetmealService;
import com.itheima.service.Impl.ImageServiceImpl;
import com.itheima.servlet.FreeMarker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐管理")
public class CheckSetmealController {
	@Autowired
	private CheckSetmealService checkSetmealService;
	@Autowired
	private AliOssUtil aliOssUtil;
	@Autowired
	private ImageServiceImpl imageService;
//	@Autowired
//	private FreeMarker freeMarker;

	@PostMapping("/add")
	@ApiOperation("添加套餐")
	public Result add(@RequestBody Setmeal setmeal, String checkGroupIds) {
		try {
			checkSetmealService.add(setmeal, checkGroupIds);
			log.info("新增套餐成功");
//			freeMarker.crateSetmealListFreeMarker();
//			freeMarker.crateSetmealDetailFreeMarker(setmeal.getId());
			return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
		} catch (Exception e) {
			log.info("新增套餐失败");
			return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
		}

	}

	@GetMapping("/delete")
	@ApiOperation("根据id删除套餐")
	public Result delete(Integer id) {
		try {
			checkSetmealService.delete(id);
			log.info("删除套餐成功");
			return new Result(true, "删除成功");
		} catch (Exception e) {
			log.info("删除套餐失败");
			return new Result(false, "删除失败");
		}
	}

	@GetMapping("/edit")
	@ApiOperation("修改套餐")
	public Result edit(@RequestBody Setmeal setmeal, String checkGroupIds) {
		try {
			checkSetmealService.edit(setmeal, checkGroupIds);
			log.info("编辑套餐成功");
			return new Result(true, "修改成功");
		} catch (Exception e) {
			log.info("编辑套餐失败");
			return new Result(false, "修改失败");
		}
	}

	@PostMapping("/findPage")
	@ApiOperation("分页查询")
	public PageResult findPage(@RequestBody QueryPageBean setmeal) {
		log.info("分页查询套餐");
		PageResult pageResult = checkSetmealService.findPage(setmeal);
		return pageResult;
	}

	@GetMapping("/findById")
	@ApiOperation("根据id查询套餐")
	public Result findById(Integer id) {
		Setmeal setmeal = checkSetmealService.findDetailById(id);
		return new Result(true, "查询成功", setmeal);
	}

	@PostMapping("/upload")
	@ApiOperation("上传图片")
	public Result file(MultipartFile file) {
		try {
			//获取原始文件名
			String originalFilename = file.getOriginalFilename();
			//获取文件后缀名
			String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
			//构造文件名
			String objectName = UUID.randomUUID().toString() + suffix;
			//调用阿里云文件上传工具类，获取文件的请求路径并上传图片
			String url = aliOssUtil.upload(file.getBytes(), objectName);
			//将上传的图片地址保存到redisd的uploadedImages中
			imageService.saveUploadedImages(url);
			return new Result(true, "上传成功", url);
		} catch (IOException e) {
			e.printStackTrace();
			log.info("文件上传失败：{}", e.getMessage());
		}
		return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
	}
}
